package com.japher.hackson.activity;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.common.views.LoadingDialog;
import com.common.image_loader.LocalImageManager;
import com.japher.hackson.R;
import com.japher.hackson.utils.AlertMessage;
import com.japher.hackson.utils.CameraUtil;

public class EditActivity extends Activity implements OnClickListener {
    
    private Button mBtnSend;
    int reaminCount;
    int MAX_COUNT = 130;
    private TextView mTextRemain;
    private EditText mEditText;
    LoadingDialog mLoadingView;
    private ImageView mAddImage;
    private ImageView mPhoto;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_edit);
        mTextRemain = (TextView) findViewById(R.id.tx_reamin);
        mAddImage = (ImageView) findViewById(R.id.add_img); 
        mAddImage.setOnClickListener(this);
        mPhoto = (ImageView) findViewById(R.id.photo); 
        mLoadingView = new LoadingDialog(this, false, null);
        mLoadingView.setMessage("正在加载数据...");
        
        mBtnSend = (Button) findViewById(R.id.tx_right_btn);
        mBtnSend.setBackground(getResources().getDrawable(R.drawable.btn_style_one));
        mBtnSend.setOnClickListener(this);
        mBtnSend.setVisibility(View.VISIBLE);
        mBtnSend.setText("发表");
        mEditText = (EditText) findViewById(R.id.edit_comment);
        
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() >= 0) {
                    reaminCount = MAX_COUNT - s.length();
                    // if(s.length() >= 6)
                    // {
                    // getbtn_right().setBackgroundDrawable(mDrawableReady);
                    // getbtn_right().setEnabled(true);
                    // }
                    // else
                    // {
                    // getbtn_right().setEnabled(false);
                    // getbtn_right().setBackgroundDrawable(mDrawableNotReady);
                    // }
                }
                
            }
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
            }
            
            @Override
            public void afterTextChanged(Editable s) {
                String priceStr = "";
                if (reaminCount < 0) {
                    priceStr = "已超出<font color=\"#FF0000\">" + Math.abs(reaminCount) + "</font>"
                        + "个字";
                } else {
                    priceStr = "还可以输入<font color=\"#5fc13d\">" + reaminCount + "</font>" + "个字";
                }
                
                mTextRemain.setText(Html.fromHtml(priceStr));
                
            }
        });
        
        mTextRemain
            .setText(Html.fromHtml("还可以输入<font color=\"#5fc13d\">" + 130 + "</font>" + "个字"));
    }
    
    private boolean parseUserInput() {
        boolean result = true;
        if (mEditText != null && mEditText.length() > 130) {
            AlertMessage.show(this, "字数超过130字了啦~");
            result = false;
        } else if (mEditText != null && mEditText.length() < 6) {
            AlertMessage.show(this, "字数至少6个字符");
            result = false;
        }
        return result;
    }
    
    private  Handler handler = new Handler();
    @Override
    public void onClick(View v) {
        
        if(v == mAddImage){
            CameraUtil.takePhotoFromAlbum(EditActivity.this, CameraUtil.PHOTOPICKALBUM);
        }else{
          //parseUserInput();
            mLoadingView.show();
            
            Runnable r = new Runnable() {
                
                @Override
                public void run() {
                    AlertMessage.show(EditActivity.this, "发表成功");
                    mLoadingView.dismiss();
                    finish();
                }
            };
            
            handler.postDelayed(r, 2000);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK)
        {

            int width = this.getResources().getDisplayMetrics().widthPixels;
            float density = this.getResources().getDisplayMetrics().density;
            
            int widthDp = (int)((width - 0.5f) / density);
            
            int w = (int) (widthDp / 125f);
            
            // 拍照
            if (requestCode == CameraUtil.PHOTOHRAPH)
            {
                 File picture = new File(CameraUtil.camerPath);
                 if(picture.exists())
                 {
                     CameraUtil.zoomPhoto(Uri.fromFile(picture), this, widthDp,125, w,1,CameraUtil.PHOTORESOULT);
                     return;
                 }
            }
            
            if (data == null)
                return;
            
            // 读取相册缩放图片
            if (requestCode == CameraUtil.PHOTOPICKALBUM)
            {
                CameraUtil.zoomPhoto(data.getData(), this, widthDp, 125, w,1,CameraUtil.PHOTORESOULT);
                return;
            }

            // 处理结果
            if (requestCode == CameraUtil.PHOTORESOULT)
            {
                    Bitmap photo = LocalImageManager.loadImage("zoom.jpg");
                    mPhoto.setVisibility(View.VISIBLE);
                    mPhoto.setImageBitmap(photo);
                    mPhoto.invalidate();
            }
        }
    }
}
